package com.example.springboot.moduleone.pojo;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {
    private module user;
    private List<String> permissions;
    @JSONField(serialize = false)
    private List<GrantedAuthority> authorities;

    public LoginUser(module user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null) {
            return authorities;
        }
//        authorities = new ArrayList<GrantedAuthority>();
//        // 封装权限信息 -》GrantedAuthority
//        permissions.forEach(s -> {
//            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(s);
//            authorities.add(simpleGrantedAuthority);
//        });

        authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return authorities;
    }
   //用戶密碼
    @Override
    public String getPassword() {
        return user.getPassword();
    }
    //用戶名
    @Override
    public String getUsername() {
        return user.getUsername();
    }
    //用戶是否可用
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //用戶是否被鎖定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //用戶密碼是否過期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //用戶是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}